<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" style="width: 250px;" placeholder="请输入订单编号" clearable
                    maxlength="30" show-word-limit @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="预约医生" prop="expertName">
                <el-input v-model="queryParams.expertName" style="width: 250px;" placeholder="请输入订单编号" clearable
                    maxlength="10" show-word-limit @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="医生联系方式" prop="expertContact">
                <el-input v-model="queryParams.expertContact" style="width: 250px;" placeholder="请输入医生联系方式" clearable
                    maxlength="11" show-word-limit @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="预约人" prop="recordName">
                <el-input v-model="queryParams.recordName" style="width: 250px;" placeholder="请输入预约人" clearable
                    maxlength="10" show-word-limit @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="预约人电话" prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" style="width: 250px;" placeholder="请输入预约人电话" clearable
                    maxlength="11" show-word-limit @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="预约时间" prop="appointmentDate">
                <el-date-picker v-model="queryParams.appointmentDate" type="date" format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="咨询类型" prop="consultationType">
                <el-select v-model="queryParams.consultationType">
                    <el-option v-for="item in consultationTypeList" :key="item.id" :value="item.id"
                        :label="item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="订单状态" prop="appointmentStatus">
                <el-select v-model="queryParams.appointmentStatus">
                    <el-option v-for="item in statusList" :key="item.id" :value="item.id"
                        :label="item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini"  @click="handleExport"
                    v-hasPermi="['business:order:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="wxOrderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" width="55" type="index" />
            <el-table-column label="订单编号" align="center" prop="orderNo" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="预约人" align="center" prop="recordName" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="预约人电话" align="center" prop="phoneNumber" min-width="150"
                :show-overflow-tooltip="true" />

            <el-table-column label="性别" align="center" prop="gender" min-width="100" :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <div v-if="scope.row.gender == 0">男</div>
                    <div v-if="scope.row.gender == 1">女</div>
                    <div v-if="scope.row.gender == 2">未知</div>
                </template>
            </el-table-column>
            <el-table-column label="咨询方式" align="center" prop="consultationMethod" min-width="100"
                :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <div v-if="scope.row.consultationMethod == 1">电话咨询</div>
                    <div v-if="scope.row.consultationMethod == 2">线上语音</div>
                    <div v-if="scope.row.consultationMethod == 3">线上视频</div>
                    <div v-if="scope.row.consultationMethod == 4">当面咨询</div>
                </template>
            </el-table-column>
            <el-table-column label="支付金额" align="center" prop="amount" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="退款金额" align="center" prop="refundAmount" min-width="150"
                :show-overflow-tooltip="true" />

            <el-table-column label="预约医生" align="center" prop="expertName" min-width="100"
                :show-overflow-tooltip="true">

            </el-table-column>
            <el-table-column label="医生联系方式" align="center" prop="expertContact" min-width="150"
                :show-overflow-tooltip="true">

            </el-table-column>
            <el-table-column label="预约时间" align="center" prop="appointmentDate" width="220">
                <template slot-scope="scope">
                    <span>{{ scope.row.appointmentDate }} {{ scope.row.startTime }} - {{ scope.row.endTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单状态" align="center" prop="appointmentStatus" min-width="100"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.appointmentStatus == 1">待支付</div>
                    <div v-if="scope.row.appointmentStatus == 2">待咨询</div>
                    <div v-if="scope.row.appointmentStatus == 3">已完成</div>
                    <div v-if="scope.row.appointmentStatus == 4">已取消</div>
                    <div v-if="scope.row.appointmentStatus == 5">已退款</div>
                    <div v-if="scope.row.appointmentStatus == 6">退款中</div>
                </template>
            </el-table-column>
            <el-table-column label="咨询类型" align="center" prop="consultationType" min-width="100"
                :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <div @click="showDetail(scope.row.recordId)">
                        <el-tag type="success" v-if="scope.row.consultationType == 1">个人</el-tag>
                        <el-tag type="warning" v-if="scope.row.consultationType == 2">家庭</el-tag>
                    </div>

                </template>
            </el-table-column>
            <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.submitTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-if="scope.row.appointmentStatus == 6 || scope.row.appointmentStatus == 2"
                        v-hasPermi="['business:wxOrder:back']">退款</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改微信商品订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="退款比例" prop="radios">
                    <el-select v-model="form.radios" @change="selectRadio">
                        <el-option v-for="item in radioList" :key="item.id" :value="item.id" :label="item.name">

                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="退款金额" prop="refundAmount">
                    <el-input-number v-model="form.refundAmount" :min="0" :max="1000" :controls="false" :precision="2"
                        :step="0.1" label="请输入退款金额"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>


        <!-- 在模板中添加弹窗 -->
        <el-dialog title="个人档案" :visible.sync="dialogVisible" width="70%">
            <el-card class="box-card">
                <div class="infos">患者名称：{{ form.recordName }}</div>
                <div class="infos">患者关系：{{ form.recordIdentity }}</div>
                <div class="infos">联系方式：{{ form.phoneNumber }}</div>
                <div class="infos">创建日期：{{ form.createTime }}</div>
                <div class="infos">性别：{{ form.gender == 0 ? '男' : '女' }}</div>
            </el-card>
        </el-dialog>
    </div>
</template>

<script>
import { listWxOrder, getWxOrder, delWxOrder, addWxOrder, updateWxOrder, appointment } from "@/api/wxOrder/wxOrder";

export default {
    name: "WxOrder",
    data() {
        return {
            dialogVisible: false,
            rowItem: {},
            statusList: [
                { id: 1, name: '待支付' },
                { id: 2, name: '待咨询' },
                { id: 3, name: '已完成' },
                { id: 4, name: '已取消' },
                { id: 5, name: '已退款' },
                { id: 6, name: '退款中' }
            ],
            consultationTypeList: [
                { id: 1, name: '个人' },
                { id: 2, name: '家庭' },
            ],
            radioList: [
                {
                    id: 1,
                    name: '50%'
                },
                {
                    id: 2,
                    name: '80%'
                },
                {
                    id: 3,
                    name: '100%'
                }
            ],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 微信商品订单表格数据
            wxOrderList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                appointmentDate: null,
                expertName: null,
                expertContact: null,
                recordName: null, phoneNumber: null,
                pageNum: 1,
                pageSize: 10,
                orderNo: null, consultationType: null, appointmentStatus: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                radios: [
                    { required: true, message: "退款比例不能为空", trigger: "blur" }
                ],
                refundAmount: [
                    { required: true, message: "退款金额不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        showDetail(id) {
            appointment(id).then(res => {
                this.dialogVisible = true;
                this.form = res.data
            })

        },
        selectRadio(e) {
            if (e == 1) {
                this.form.refundAmount = (Number(this.rowItem.amount) * 0.5)
            }
            if (e == 2) {
                this.form.refundAmount = (Number(this.rowItem.amount) * 0.8)
            }
            if (e == 3) {
                this.form.refundAmount = (Number(this.rowItem.amount) * 1)
            }
        },
        /** 查询微信商品订单列表 */
        getList() {
            this.loading = true;
            listWxOrder(this.queryParams).then(response => {
                this.wxOrderList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                refundAmount: null,
                radios: null,
                penaltyAmount: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加微信商品订单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.rowItem = row
            this.open = true;
            this.title = "退款";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.penaltyAmount = (Number(this.rowItem.amount) - Number(this.form.refundAmount))
                    this.form.id = this.rowItem.id
                    updateWxOrder(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除微信商品订单编号为"' + ids + '"的数据项？').then(function () {
                return delWxOrder(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/business/appointment/export', {
                ...this.queryParams,
                ids:this.ids
            }, `预约订单_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style lang="scss" scoped>
.infos {
    margin-top: 20px;
}
</style>
